

local socket = require "socket"
local cjson = require "cjson"

local cc = socket.connect("127.0.0.1", 8888)
cc:settimeout(1)

local last
local result = {}

local function dispatch()
	while true do
		local status
		local line, error = cc:receive('*l')
		
		if not line then
			break
		end
		local result = cjson.decode(line)
		print("Response:",session, result[1], result[2])
	end
end

local session = 0

local function send_request(v)
	local str = cjson.encode(v)
	print(cc:send(str.."\n"))
	print("Request:", str)
end

while true do
	dispatch()
	local cmd = io.read()
	if cmd then
		send_request({cmd})
--		local args = {}
--		string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end )
--		send_request(args)
	else
		socket.sleep(1)
	end
end